package handler

import (
	"github.com/gin-gonic/gin"
	"github.com/lab-online/internal/course/constant"
	"github.com/lab-online/internal/course/entity"
	mw "github.com/lab-online/pkg/middleware"
	"github.com/lab-online/pkg/resp"
	"github.com/lab-online/pkg/util"
)

type GetExperimentsReqParams struct {
	ID uint `uri:"id" binding:"required" example:"1"` // 课程id
}

var GetExperimentsValidator = mw.NewValidator(func() *mw.ValidatorOptions {
	return &mw.ValidatorOptions{
		Params: &GetExperimentsReqParams{},
	}
})

type GetExperimentsRespBody struct {
	resp.BaseRespBody
	Data []SearchExperimentRespData `json:"data" binding:"required"`
}

// GetExperiments 查询实验
//
//	@Summary				查询实验
//	@Description			所有人可以查询某个课程的实验列表
//	@Description.markdown	GetExperiments
//	@Tags					course
//	@Accept					json
//	@Produce				json
//	@Security				bearer
//	@Param					Authorization	header		string	true	"`all`"
//	@Param					id				path		uint	true	"课程id"
//	@Success				201				{object}	GetExperimentsRespBody
//	@Failure				400				{object}	resp.FailedRespBodyString
//	@Failure				403				{object}	resp.FailedRespBodyString
//	@Failure				500				{object}	resp.FailedRespBodyString
//	@x-apifox-status		"developing"
//	@Router					/v1/courses/:id/experiments	[get]
func (h *Handler) GetExperiments(c *gin.Context) {
	courseID := c.MustGet(mw.KeyParams).(*GetExperimentsReqParams).ID
	courseEntity := entity.NewCourseEntity(entity.WithID(courseID))

	if course, err := h.domain.GetCourseDetail(courseEntity); err != nil {
		resp.ErrorHandler(c, err)
	} else {
		resp.Success(c, constant.CodeGetExperimentsSuccess,
			util.SliceMap(course.GetExperiments(),
				func(experiment entity.Experiment) *SearchExperimentRespData {
					return &SearchExperimentRespData{
						ID:          experiment.GetID(),
						Name:        experiment.GetName(),
						Description: experiment.GetDescription(),
					}
				}),
		)
	}
}
